package com.shure.surdes.common.result;

import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName CodeMsg
 * @Description 统一结果返回类
 * @Author shure
 * @Date 2023/3/16 17:29
 * @Version 1.0
 **/
@Data
public class R<T> implements Serializable {

    private int code;
    private String msg;
    private T data;

    //通用异常
    public static R<Object> SUCCESS = new R<>(0, "success");
    public static R<Object> SERVER_ERROR = new R<>(500, "服务端异常");
    //注意  %s ，格式化字符串
    //public static CodeMsg SERVER_ERROR_MSG = new CodeMsg<>(500, "服务端异常:%s");

    public R() {
    }

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public R(T data) {
        this.code = SUCCESS.code;
        this.msg = SUCCESS.msg;
        this.data = data;
    }

    /**
     * @return 异常CodeMsg 对象生成方法
     * @created 23:03 2018/8/24
     * @author Shure
     * @params
     */

    public R<Object> fillArgs(Object... args) {
        int code = this.code;
        String message = String.format(msg, args);
        return new R<>(code, message);
    }
}
